package com.atguigu.f_test;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
    双列集合存储自定义对象并遍历

    技巧 : 提前设计好双列集合 再动手  <学号,学生对象> -> <String,Student>
 */
public class Demo {
    public static void main(String[] args) {
        HashMap<String, Student> hm = new HashMap<>();

        //添加元素
        hm.put("guigu001",new Student("林青霞",18));
        hm.put("guigu002",new Student("张曼玉",17));
        hm.put("guigu003",new Student("王祖贤",19));

        //遍历
        //键找值
        Set<String> keys = hm.keySet();
        for (String key : keys) {
            Student student = hm.get(key);
            //取出学生对象的姓名和年龄
            String name = student.getName();
            int age = student.getAge();
            System.out.println("学号是 : " + key + " , 学生姓名是 : " +name+ " , 学生年龄是 : " + age);
        }
        System.out.println("----------------------------");
        //键值对
        Set<Map.Entry<String, Student>> entries = hm.entrySet();
        for (Map.Entry<String, Student> entry : entries) {
            //根据entry取出键和值
            String key = entry.getKey();
            Student student = entry.getValue();
            //取出学生对象的姓名和年龄
            String name = student.getName();
            int age = student.getAge();
            System.out.println("学号是 : " + key + " , 学生姓名是 : " +name+ " , 学生年龄是 : " + age);
        }
    }
}
